package org.platform.processor.knowledge;

import com.alibaba.fastjson.JSON;
import com.mc.mic.core.exception.IllegalParamException;
import org.platform.constant.DiagnoseTypeConstant;
import org.platform.constant.VersionConstant;
import org.platform.dao.mapper.FdKbKnowledgemainMapper;
import org.platform.mo.csmo.cmd10022.KnowApplyRequestParam;
import org.platform.mo.csmo.common.KB_Main;
import org.platform.mo.csmo.common.KnowMonitorApplyBean;
import org.platform.mo.csmo.common.KnowTestApplyBean;
import org.platform.mo.csmo.common.KnowTreeApplyBean;
import org.platform.mo.db.FdKbKnowledgemain;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author FengJie
 * @date 2018/6/22
 */
@Service
public class KnowledgeProcessor {
    @Autowired
    private FdKbKnowledgemainMapper knowledgeMainMapper;
    @Autowired
    private EventProcessor eventProcessor;
    @Autowired
    private TestProcesor testProcesor;
    @Autowired
    private TreeProcessor treeProcessor;

    /**
     * 更新知识主表版本(审核成功)
     * * 1.把所有的知识修改为不可用的状态
     * * 2.把当前的知识设置为可用的状态
     */
    public void renewVersion(KB_Main kbMain) {
        //全部设置为不可用状态
        FdKbKnowledgemain unUserMain = new FdKbKnowledgemain();
        unUserMain.setUserby(VersionConstant.UNUSE);
        Example example = new Example(FdKbKnowledgemain.class);
        example.createCriteria().andEqualTo("userby", VersionConstant.USE);
        knowledgeMainMapper.updateByExampleSelective(unUserMain, example);

        //把当前设置为可用
        kbMain.setUserby(VersionConstant.USE);
        renewStatus(kbMain);
    }

    /**
     * 更新知识状态(审核未通过)
     *
     * @param kbMain
     */
    public void renewStatus(KB_Main kbMain) {
        FdKbKnowledgemain main = new FdKbKnowledgemain();
        BeanUtils.copyProperties(kbMain, main);
        knowledgeMainMapper.updateByPrimaryKeySelective(main);
    }

    /**
     * 新增知识
     *
     * @param main
     */
    public void addMain(KB_Main main) {
        FdKbKnowledgemain knowledgeMain = new FdKbKnowledgemain();
        BeanUtils.copyProperties(main, knowledgeMain);
        knowledgeMainMapper.insert(knowledgeMain);
    }

    /**
     * 主要把接收申请的数据入库
     */
    public void addKnowledge(KnowApplyRequestParam applyParam) {
        int knowledgeType = applyParam.getKnowledgeType();
        String knowledgeCtx = applyParam.getKnowledgeCtx();
        addMain(applyParam.getKbMain());
        switch (knowledgeType) {
            case DiagnoseTypeConstant.KNOW_MONITOR:
                KnowMonitorApplyBean monitorApplyBean = JSON.parseObject(knowledgeCtx, KnowMonitorApplyBean.class);
                eventProcessor.addKnowMonitor(monitorApplyBean);
                break;
            case DiagnoseTypeConstant.KNOW_TEST:
                KnowTestApplyBean testApplyBean = JSON.parseObject(knowledgeCtx, KnowTestApplyBean.class);
                testProcesor.addKnowTest(testApplyBean);
                break;
            case DiagnoseTypeConstant.KNOW_TREE:
                KnowTreeApplyBean treeApplyBean = JSON.parseObject(knowledgeCtx, KnowTreeApplyBean.class);
                treeProcessor.addKnowTree(treeApplyBean);
                break;
            default:
                throw new IllegalParamException("申请类型不正确 :未找到指定的知识申请类型");
        }
    }

    /**
     * 根据知识主表,去查询对应的子模块
     *
     * @param main
     * @return
     */
    public KnowApplyRequestParam getKnowledgeByMain(KB_Main main) {
        KnowApplyRequestParam requestParam = new KnowApplyRequestParam();
        Integer knowledgeType = main.getKnowledgetype();
        switch (knowledgeType) {
            case DiagnoseTypeConstant.KNOW_MONITOR:
                KnowMonitorApplyBean monitorApplyBean = eventProcessor.getKnowMonitorByKBMain(main);
                requestParam.setKnowledgeCtx(JSON.toJSONString(monitorApplyBean));
                break;
            case DiagnoseTypeConstant.KNOW_TEST:
                KnowTestApplyBean testApplyBean = testProcesor.getKnowTestByKBMain(main);
                requestParam.setKnowledgeCtx(JSON.toJSONString(testApplyBean));
                break;
            case DiagnoseTypeConstant.KNOW_TREE:
                KnowTreeApplyBean treeApplyBean = treeProcessor.getKnowTreeByKBMain(main);
                requestParam.setKnowledgeCtx(JSON.toJSONString(treeApplyBean));
                break;
            default:
                throw new IllegalParamException("参数不正确 :未找到指定的知识申请类型");
        }
        return requestParam;
    }

    /**
     * 根据知识类型,获取当前知识的最可用高版本号
     *
     * @param knowledgeType 知识类型
     * @return 当前最高的版本号
     */
    private Double getMaxVersionByKnowledgeType(Integer knowledgeType) {
        Example example = new Example(FdKbKnowledgemain.class);
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("version").desc();
        criteria.andEqualTo("knowledgetype", knowledgeType);
        criteria.andEqualTo("userby", 0);
        List<FdKbKnowledgemain> knowledgemainList = knowledgeMainMapper.selectByExample(example);
        return knowledgemainList.get(0).getVersion();
    }

    /**
     * 根据知识ID,获取当前知识的最高可用版本号
     *
     * @param knowledgeId 知识id
     * @return 当前最高的版本号
     */
    public Double getMaxVersionByKnowledgeId(String knowledgeId) {
        Example example = new Example(FdKbKnowledgemain.class);
        Example.Criteria criteria = example.createCriteria();
        example.orderBy("version").desc();
        criteria.andEqualTo("knowledgeid", knowledgeId);
        criteria.andEqualTo("userby", 0);
        List<FdKbKnowledgemain> knowledgemainList = knowledgeMainMapper.selectByExample(example);
        return knowledgemainList.get(0).getVersion();
    }

}
